/*
 * @Author: silence
 * @Date: 2023-03-08 11:33:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-31 09:50:10
 * @Description: 
 */

import 'package:sil_base_widget_package/tools/SilUtilsTools.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

import '../../bloc/userbloc/user_bloc.dart';
import '../../entities/UserEntity.dart';
import '../../main.dart';
import '../../utils/device/DeviceInfo.dart';
import '../ApiUtils.dart';
import '../R.dart';

/**
 * 用户注册
 */
Future<R> api_user_register(
    {String? username,
    String? code,
    String? password,
    String? country,
    String? areaCode}) async {
  Map<String, dynamic> map = Map();

  if (!SilTools.stringIsEmpty(username)) map['username'] = username;
  if (!SilTools.stringIsEmpty(code)) map['code'] = code;
  if (!SilTools.stringIsEmpty(password)) map['password'] = password;
  // if (!SilTools.stringIsEmpty(country)) map['country'] = country;
  if (!SilTools.stringIsEmpty(areaCode)) map['country'] = areaCode;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, 'api/user/register',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 用户登录
 */
Future<R> api_user_login(
    {String? phone, String? email, String? code, String? password}) async {
  Map<String, dynamic> map = Map();

  if (!SilTools.stringIsEmpty(phone)) {
    // type：1手机
    map['type'] = '1';
    map['username'] = phone;
  } else if (!SilTools.stringIsEmpty(email)) {
    // type：2邮箱
    map['type'] = '2';
    map['username'] = email;
  }

  if (!SilTools.stringIsEmpty(code)) {
    // 验证码登录
    map['code'] = code!;
    map['loginType'] = '1';
  } else if (!SilTools.stringIsEmpty(password)) {
    // 密码登录
    map['password'] = password!;
    map['loginType'] = '2';
  }

  // 唯一标识符
  String? udid = await DeviceInfo.deviceUniqueId();
  if (!SilTools.stringIsEmpty(udid)) {
    map['Authorization'] = udid;
  }

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, 'api/user/login',
      param: map);

  // 登录的时候，删除token
  String? token = await UserBlocTool.getUserAuth();
  if (!SilUtilsTools.stringIsEmpty(token)) {
    UserBlocTool.removeAuth();
  }

  R r = R.fromData(methodAwait, handlerData: (map) {
    // SilEntityLog.log('UserEntity', map['userInfo']);
    Map resultMap = map;
    if (resultMap.containsKey('userInfo')) {
      UserEntity userEntity = UserEntity.fromJson(resultMap['userInfo']);
      UserBlocTool.update(navigatorKey.currentState!.context, userEntity);
    }

    if (resultMap.containsKey('token')) {
      token = resultMap['token'];
    }

    return map;
  });

  // 保存身份票据
  if (!SilTools.stringIsEmpty(token)) {
    await UserBlocTool.saveAuth(token!);
  }

  return r;
}

/**
 * 设置密码
 */
Future<R> api_user_set_password({
  String? phone,
  String? email,
  required String password,
  required String? code,
}) async {
  Map<String, dynamic> map = Map();

  // 密码
  map['password'] = password;
  // 验证码登录
  map['code'] = code;

  if (!SilTools.stringIsEmpty(phone)) {
    // type：1手机
    map['type'] = '1';
    map['username'] = phone;
  } else if (!SilTools.stringIsEmpty(email)) {
    // type：2邮箱
    map['type'] = '2';
    map['username'] = email;
  }

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, "/api/user/updatePas",
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 绑定/修改手机号
 * phone 新手机号
 * code  验证码
 * areaCode  区号
 */
Future<R> api_user_change_phone(
    {required String phone, required String code, String? areaCode}) async {
  Map<String, dynamic> map = Map();
  map['phone'] = phone;
  map['code'] = code;

  // 区号
  if (!SilTools.stringIsEmpty(areaCode)) {
    map['areaCode'] = areaCode;
  }

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/user/updatePhone',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 修改邮箱
 */
Future<R> api_user_change_email(
    {required String email, required String code}) async {
  Map<String, dynamic> map = Map();
  map['email'] = email;
  map['code'] = code;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/user/updateEmail',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 修改用户基础信息
 * avatarUrl 用户头像
 * nickName  用户昵称
 * country  国家id
 * countryName 国家名称
 */
Future<R> api_user_info_modify(
    {String? userId,
    String? avatarUrl,
    String? nickName,
    String? countryId,
    String? countryName}) async {
  Map<String, dynamic> map = Map();
  if (userId != null) map['id'] = userId;
  if (avatarUrl != null) map['avatar'] = avatarUrl;
  if (nickName != null) map['nickName'] = nickName;
  if (countryId != null) map['country'] = countryId;
  if (countryName != null) map['countryName'] = countryName;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PutMethd, '/api/canUser/canUser',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 注销账号
 */
Future<R> api_user_destroy_account({required String? userId}) async {
  Map<String, dynamic> map = Map();

  if (userId != null) map['id'] = userId;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/user/destroyAccount',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    return map;
  });

  return r;
}

/**
 * 用户信息
 */
Future<R> api_userInfo_info() async {
  Map<String, dynamic> params = Map();
  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.GetMethod, '/api/user/findLoginInfo',
      param: params);

  R r = R.fromData(methodAwait, throwException: true, handlerData: (map) {
    UserEntity userEntity = UserEntity.fromJson(map);
    UserBlocTool.update(navigatorKey.currentState!.context, userEntity);

    return userEntity;
  });

  return r;
}

/// 检测注销帐号状态
Future<R> api_is_logoff() async {
  dynamic methodAwait = await ApiUtils.methodAwait(
    ApiUtils.PostMethd,
    'api/v1.user/islogoff',
  );

  return R.fromData(methodAwait);
}

/// 绑定推送设备的cId
Future<R> api_bind_push_cid(String cId) async {
  Map<String, dynamic> params = Map();
  // 更换成极光推送，使用新的参数，替换掉原来的个推使用的cid
  params["cId"] = cId;
  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.GetMethod, 'api/user/bindingCidJg',
      param: params);

  return R.fromData(methodAwait);
}
